<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getAdd()
    {
        
        return view('admin.user.add');
    }

    //执行数据插入操作
    public function postInsert(Request $request)
    {
        // dd($request->all());
        // 执行数据检测
        // if(!$request->input('username')){
        //     return back()->with('error','用户名未填写 ');
        // }

        // 数据验证
        $this->validate($request,[
            'email' => 'required|unique:dota_user|max:50',
            'password' => 'required',
            'phone' => 'required',
            'id_card' => 'required',
            'username' => 'required',
            'pic' => 'image'
            ],[

            'email.required'=>'用户账号不能为空',
            'email.unique'=>'用户已存在',
            'email.max'=>'用户名过长',
            'password.required'=>'用户密码不能为空',
            'phone.required'=>'手机号不能为空',
            'id_card.required'=>'身份证号不能为空',
            'username.required'=>'游戏昵称不能为空',
            'pic.image'=>'图片格式不对'
        ]);

         if($request->hasFile('pic')){
            //随机文件名
            $name = time()+rand(1,99);

             //获取后缀名
            $ext = $request->file('pic')->getClientOriginalExtension();
            echo $name.'.'.$ext;
            //检测后缀名
            $arr = array('jpg','png','jpeg');
            //判断
            if(!in_array($ext,$arr)){
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://la.cn/test/add";
                </script>';
            }
            $request->file('pic')->move('./public/admin/userimage/',$name.'.'.$ext);
            //echo '<img src="/uploads/'.$name.'.jpg" alt="">';
           
            $img = self::suofang('./public/admin/userimage/'.$name.'.'.$ext,30,30);
            // dd($data['pic']);


        }
        
        $rand = DB::table('dota_user')->max('dota_id');

        $data['dota_id'] = $rand + 1;
            

        //都验证成功
        $data = $request->only(['email','password','phone','id_card','username','pic']);

        $data['token'] = str_random(50);
        $data['password'] = Hash::make($data['password']);
        $data['pic'] = $name.'.'.$ext;
        //执行插入
        $res = DB::table('dota_user')->insert($data);
        //判断是否添加成功
        if($res){
            return redirect('/admin/user/index')->with('success','用户添加成功');
        }else{
            return back()->with('error','用户修改失败');
        }
        

        
    }

    //用户列表页
    public function getIndex(Request $request)
    {
        //查询所有用户数据
        // $users = DB::table('dota_user')->get();
        //获取分页大写
        $num = $request->input('num',10);
        if($request->input('seek')){
             $users = DB::table('dota_user')->where('email','like','%'.$request->input('seek').'%')->paginate($num);
        }else{
             $users = DB::table('dota_user')->paginate($num);
            }
        //获取所有数据
        $data = $request->all();
        // dd($data);
        // dd($users);
        //解析模板
        return view('admin.user.index',['users'=>$users,'request'=>$data]);
    }


    //=====================================================================修改表单
    public function getEdit($id)
    {
        //查询数据
        $user = DB::table('dota_user')->where('id',$id)->first();
        // dd($user);
        //解析模板
        return view('admin.user.edit',['user'=>$user]);
    }

    public function postUpdate(Request $request)
    {
        $id = $request->only(['id']);
        $data = $request ->only(['username','sex','qx','pic']);

        //++++========================+++++++++++++++++++++++++++++++++=上传
         if($request->hasFile('pic')){
            //随机文件名
            $name = time()+rand(1,99);

             //获取后缀名
            $ext = $request->file('pic')->getClientOriginalExtension();
            echo $name.'.'.$ext;
            //检测后缀名
            $arr = array('jpg','png','jpeg');
            //判断
            if(!in_array($ext,$arr)){
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://la.cn/test/add";
                </script>';
            }
            $request->file('pic')->move('./public/admin/userimage/',$name.'.'.$ext);
            //echo '<img src="/uploads/'.$name.'.jpg" alt="">';
           
            $img = self::suofang('./public/admin/userimage/'.$name.'.'.$ext,30,30);
            // dd($data['pic']);


        }
        


        $data['pic'] = $name.'.'.$ext;
      $res = DB::table('dota_user')->where('id',$id)->update($data);
      
        if($res){
            return redirect('/admin/user/index')->with('success','用户修改成功');
        }else{
            return back()->with('error','用户修改失败');
        }

    } 

    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++用户删除
        public function getDelete(Request $request,$id)
        {
            $res = DB::table('dota_user')->where('id',$id)->delete();
           
        if($res){
            return redirect('/admin/user/index')->with('success','用户删除成功');
        }else{
            return back()->with('error','用户删除失败');
        }
        }



    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++缩放类
    public static function suofang($spath, $sw = 100, $sh = 100, $pre = 's_')
{
    //1.创建画布
    //计算比例  100 100  要缩放的图像的宽高  1920 1200  //用小的比例
    $srcinfo = getimagesize($spath);
    //根据图片类型 去创建图像资源 ...
    $src = imagecreatefromjpeg($spath); //strrchr  aaaa.jpg
    
    if($sw/$srcinfo[0] > $sh/$srcinfo[1]){
        $bili = $sh/$srcinfo[1];
    }else{
        $bili = $sw/$srcinfo[0];
    }
    //舍去法取整
    $sw = floor($srcinfo[0] * $bili);
    $sh = floor($srcinfo[1] * $bili);


    $dst = imagecreatetruecolor($sw,$sh);

    //3.开始绘画
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $sw, $sh, $srcinfo[0], $srcinfo[1]);

    //5.将图像保存为文件
    //随机文件名  //获取文件后缀名  目录
    //$ext = strrchr($spath,'.');
    //$filename = time().rand(1000,9999).$ext;
    $filename = rtrim(dirname($spath),'/').'/'.$pre.basename($spath);
    
    imagejpeg($dst, $filename);

    //6.销毁资源
    imagedestroy($dst);
    imagedestroy($src);
    
    //返回原文件名加前缀
    return $pre.basename($spath);
}

}
